﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using FastReflectionLib;
using SZHL.Data;
using Newtonsoft.Json;
using SZHL.UI.AppCode;
using System.Data;
using System.IO;

namespace SZHL.UI.API
{
    public class GZRZManage : IWsService
    {
        public void ProcessRequest(HttpContext context, ref Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            MethodInfo methodInfo = typeof(GZRZManage).GetMethod(msg.Action.ToUpper());
            GZRZManage model = new GZRZManage();
            methodInfo.FastInvoke(model, new object[] { context, msg, P1, P2, UserInfo });
        }
        #region 工作日志

        /// <summary>
        /// 添加工作日志
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void ADDGZRZ(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            ZH_GZRZ gzrz = JsonConvert.DeserializeObject<ZH_GZRZ>(P1);

            if (gzrz.RBContent == null)
            {
                msg.ErrorMsg = "日报内容不能为空";
                return;
            }

            if (P2 != "") // 处理微信上传的图片
            {
                string fids = CommonHelp.ProcessWxIMG(P2, UserInfo.User.UserName);
                if (!string.IsNullOrEmpty(gzrz.Files))
                {
                    gzrz.Files += "," + fids;
                }
                else
                {
                    gzrz.Files = fids;
                }
            }


            if (gzrz.ID == 0)
            {
                gzrz.CRDate = DateTime.Now;
                gzrz.CRUser = UserInfo.User.UserName;
                gzrz.CRUserName = UserInfo.User.UserRealName;
                gzrz.BranchName = new JH_Auth_BranchB().GetBranchNameByCode(UserInfo.User.BranchCode);
                new ZH_GZRZB().Insert(gzrz);
            }
            else
            {
                new ZH_GZRZB().Update(gzrz);
            }


            msg.Result = gzrz;
        }
        /// <summary>
        /// 获取日志列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">日志类型</param>
        /// <param name="P2">查询条件</param>
        /// <param name="strUserName"></param>
        public void GETGZRZLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {

            string branchCode = context.Request["b"] ?? "";
            int page = 0;
            int.TryParse(context.Request.QueryString["p"] ?? "1", out page);//页码
            page = page == 0 ? 1 : page;
            DataTable dt = new DataTable();
            int recordCount = 0;
            if (branchCode != "")
            {
                string strQueryUser = context.Request["QUSER"] ?? "";

                if (strQueryUser != "")
                {
                    dt = new ZH_GZRZB().GETGZRZList(P1, strQueryUser.ToFormatLike(), P2, "", page, ref recordCount);
                }
                else
                {
                    //如果部门参数不为空,则获取当前部门的所有用户
                    string userNames = new JH_Auth_BranchB().GetAllUserByBranch(branchCode);
                    if (userNames != "")
                    {
                        dt = new ZH_GZRZB().GETGZRZList(P1, userNames.ToFormatLike(), P2, "", page, ref recordCount);
                    }
                    else {
                       dt= new DataTable();
                    }
                }
            }
            else
            {
                dt = new ZH_GZRZB().GETGZRZList(P1, UserInfo.User.UserName, P2, "", page, ref recordCount);
            }
            msg.Result = dt;
            msg.Result1 = Math.Ceiling(recordCount * 1.0 / 8);
        }
        /// <summary>
        /// 获取日志列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">日志类型</param>
        /// <param name="P2">查询条件</param>
        /// <param name="strUserName"></param>
        public void GETALLGZRZLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            DataTable dt = new ZH_GZRZB().GETALLGZRZList(P1, P2);
            msg.Result = dt;
        }

        /// <summary>
        /// 获取工作日报菜单的日报数量
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETGZRZMENUCOUNT(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strSql = "select Count(0) from ZH_GZRZ";
            msg.Result = new ZH_GZRZB().ExsSclarSql(strSql);
            if (UserInfo.User.UserName != "")
            {
                strSql += string.Format(" where CRUser='{0}'", UserInfo.User.UserName);
                msg.Result1 = new ZH_GZRZB().ExsSclarSql(strSql);
            }

        }
        /// <summary>
        /// 删除日报
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">日报ID</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void DELGZRZBYID(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {

                if (new ZH_GZRZB().Delete(d => d.ID.ToString() == P1))
                {
                    if (new JH_Auth_TLB().Delete(d => d.MSGTLYID == P1 && d.MSGType == "GZRZ"))
                    {
                        msg.ErrorMsg = "";
                    }
                }
            }
            catch (Exception ex)
            {
                msg.ErrorMsg = ex.Message;
            }
        }
        /// <summary>
        /// 获取日报信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">日报Id</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETGZRZMODEL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            ZH_GZRZ gzrz = new ZH_GZRZB().GetEntity(d => d.ID.ToString() == P1);
            msg.Result = gzrz;
            msg.Result1 = new ZH_RCTXB().GetDTByCommand("  SELECT ID,MSGTLYID,MSGType,MSGContent,Points,CRDate,CRUser,CRUserName  FROM JH_Auth_TL WHERE MSGType='GZRZ' AND  MSGTLYID='" + P1 + "'");
            if (!string.IsNullOrEmpty(gzrz.Files))
            {
                msg.Result2 = new FT_FileB().GetEntities(d => gzrz.Files.Split(',').Contains(d.ID.ToString()));
            }
        }
        #endregion
    }
}